﻿using UnityEngine;

public partial class ListView
{
    
    //dataIndex=0~itemNode_0对应的dataIndex-1
    private void AdjustBeginStubSize()
    {
        var firstItemNode = m_ItemNodeList[0];
        if (firstItemNode.dataIndex == 0)
        {
            ListViewUtil.SetStubSize_Vertical(m_BeginStub, 0);
        }
        else
        {
            var itemPosAndSize = m_ItemPosAndSizeList[firstItemNode.dataIndex - 1];
            ListViewUtil.SetStubSize_Vertical(m_BeginStub, itemPosAndSize.bottomDis);
        }
    }

    private void AdjustEndStubSize()
    {
        var lastItemNode = m_ItemNodeList[m_ItemNodeList.Count - 1];
        if (lastItemNode.dataIndex == (m_DataCount - 1)) //对应最后一条数据
        {
            ListViewUtil.SetStubSize_Vertical(m_EndStub, 0);
        }
        else
        {
            float bottomDis = m_ItemPosAndSizeList[lastItemNode.dataIndex].bottomDis;
            float topDis = bottomDis + m_ItemSpace;
            
            var contentSize = m_ItemPosAndSizeList[m_ItemPosAndSizeList.Count - 1].bottomDis;
            ListViewUtil.SetStubSize_Vertical(m_EndStub, contentSize - topDis);
        }
    }

    //没有条目时, 填充裁剪区域所需的条目
    private void InitItems_Vertical(int firstDataIndex)
    {
        var content = m_ScrollRect.content;
        int startDataIndex = 0;
        bool isIgnoreItemSizeOnceFlag = false;
        float itemSizeSum = 0;
        if (0 == firstDataIndex)
        {
            //startDataIndex = 0;
            itemSizeSum = m_LeadPadding;
        }
        else
        {
            //顶部也要多生成一个条目, 防止穿帮
            isIgnoreItemSizeOnceFlag = true;
            startDataIndex = firstDataIndex - 1;
            itemSizeSum = 0;
        }

        float tmpItemTopDis = 0;
        if (0 == startDataIndex)
        {
            tmpItemTopDis = m_LeadPadding;
        }
        else
        {
            //从前一个条目往后摆放
            tmpItemTopDis = m_ItemPosAndSizeList[startDataIndex - 1].bottomDis + m_ItemSpace;
        }

        float vpSize = this.ViewportSize;
        for (int i = startDataIndex; i < m_DataCount; ++i)
        {
            var newItemRtf = m_OnObtainItem(this, i);
            if (null == newItemRtf)
                break;
            newItemRtf.gameObject.SetActive(true);
            newItemRtf.SetParent(content, false);
            m_ItemNodeList.Add(new ItemNode(newItemRtf, i));
                        
            float itemSize = newItemRtf.rect.height;
            float itemBottomDis = tmpItemTopDis + itemSize;
            m_ItemPosAndSizeList[i] = new ItemPosAndSize(itemSize, itemBottomDis);
            if (isIgnoreItemSizeOnceFlag)
            {
                isIgnoreItemSizeOnceFlag = false;
            }
            else
            {
                if (itemSizeSum > vpSize)
                    break;
                itemSizeSum += itemSize;
                itemSizeSum += m_ItemSpace;
            }
            tmpItemTopDis = itemBottomDis + m_ItemSpace; //下一个条目的topDis
        }

        if (m_ItemNodeList.Count > 0) //发生了添加
        {
            m_EndStub.SetAsLastSibling();
            
            m_LateUpdateFlag = 2;
            m_AutoLayoutFlag = 1;
        }
    }
    
    private void AdjustItemNodesPos_Vertical()
    {
        //再更新后续的条目
        var firstItemNode = m_ItemNodeList[0];
        var prevItemPosAndSize = m_ItemPosAndSizeList[firstItemNode.dataIndex - 1];
        
        float tmpTopDis = prevItemPosAndSize.bottomDis + m_ItemSpace;
        foreach (var itemNode in m_ItemNodeList)
        {
            int itemNodeDataIndex = itemNode.dataIndex;
            var itemPosAndSize = m_ItemPosAndSizeList[itemNodeDataIndex];
            
            float bottomDis = tmpTopDis + itemPosAndSize.size;
            itemPosAndSize.bottomDis = bottomDis;
            m_ItemPosAndSizeList[itemNodeDataIndex] = itemPosAndSize;
            
            tmpTopDis = bottomDis + m_ItemSpace;
        }
    }
    
    private void InitItemsPosAndSizeBefore_Vertical(int firstItemNodeDataIndex)
    {
        //修正前面条目的位置和大小
        var itemPosAndSize = m_ItemPosAndSizeList[firstItemNodeDataIndex];
        float tmpItemTopDis = itemPosAndSize.TopDis;
        float tmpItemBottomDis = 0;
        for (int i = 0; i < firstItemNodeDataIndex; ++i)
        {
            tmpItemBottomDis = tmpItemTopDis + m_DefaultItemSize;
            m_ItemPosAndSizeList[i] = new ItemPosAndSize(m_DefaultItemSize, tmpItemBottomDis);
            tmpItemTopDis = tmpItemBottomDis + m_ItemSpace; //下一条的top
        }

        //todo: AdjustBeginStubSize
        if (firstItemNodeDataIndex > 0)
        {
            ListViewUtil.SetStubSize_Vertical(m_BeginStub, tmpItemBottomDis);
        }
        else
        {
            ListViewUtil.SetStubSize_Vertical(m_BeginStub, 0);
        }
    }
    
    private void InitItemsPosAndSizeAfter_Vertical(int startDataIndex)
    {
        //修正后面条目的位置和大小
        var prevItemPosAndSize = m_ItemPosAndSizeList[startDataIndex - 1];
        float prevItemBottomDis = prevItemPosAndSize.bottomDis;
        float tmpItemBottomDis = prevItemBottomDis;
        for (int i = startDataIndex; i < m_DataCount; ++i)
        {
            float itemTopDis = tmpItemBottomDis + m_ItemSpace;
            tmpItemBottomDis = itemTopDis + m_DefaultItemSize;
            m_ItemPosAndSizeList[i] = new ItemPosAndSize(m_DefaultItemSize, tmpItemBottomDis);
        }
        
        //todo: AdjustEndStubSize
        float endStubSize = tmpItemBottomDis - prevItemBottomDis - m_ItemSpace;
        ListViewUtil.SetStubSize_Vertical(m_EndStub, endStubSize);
    }

    private void InitAllItemPosAndSize()
    {
        float tmpTopDis = m_ItemSpace;
        for (int i = 0; i < m_DataCount; ++i)
        {
            float bottomDis = tmpTopDis + m_DefaultItemSize;
            m_ItemPosAndSizeList.Add(new ItemPosAndSize(m_DefaultItemSize, bottomDis));
            tmpTopDis = bottomDis + m_ItemSpace;
        }
    }
    
    private void RefreshItems_Vertical(int firstDataIndex)
    {
        //清理之前的
        for (int i = 0; i < m_ItemNodeList.Count; ++i)
        {
            var itemNode = m_ItemNodeList[i];
            RecycleItem(itemNode.rtf);
        }
        m_ItemNodeList.Clear();
        m_ItemPosAndSizeList.Clear();

        if (m_DataCount <= 0)
        {
            ListViewUtil.SetStubSize_Vertical(m_BeginStub, 0);
            ListViewUtil.SetStubSize_Vertical(m_EndStub, 0);
        }
        else
        {
            InitAllItemPosAndSize();
            
            float viewportBottomDis = ViewportBottomDis;
            InitItems_Vertical(firstDataIndex);
            if (m_ItemNodeList.Count > 0)
            {
                AdjustBeginStubSize();
                AdjustItemsPosAfter_Vertical(m_ItemNodeList.Count - 1);
                AdjustEndStubSize();
            }
            ScrollToDataIndex_Vertical(firstDataIndex, 0);
            
            PrintAllItemPosAndSize("RefreshItems");
        }
    }

    private void UpdateScrollPercent_Vertical()
    {
        m_ScrollPercent = 100 - Mathf.RoundToInt(m_ScrollRect.verticalNormalizedPosition * 100);
        Debug.Log($"scrollPer: {m_ScrollPercent}");
    }

    //offset: >0为往上, <0为往下
    private void ScrollToDataIndex_Vertical(int dataIndex, float offset)
    {
        var itemPosAndSize = m_ItemPosAndSizeList[dataIndex];
        float scrollDis = itemPosAndSize.TopDis + offset;
        ListViewUtil.ContentScrollTo_Vertical(m_ScrollRect.content, scrollDis);
    }

    private int GetDataIndexByScrollDis_Vertical(float scrollDis)
    {
        int cnt = m_ItemPosAndSizeList.Count;
        if (cnt == 0)
            return 0;
        float tmpBottomDis = m_ItemPosAndSizeList[0].bottomDis;
        if (scrollDis <= tmpBottomDis)
            return 0;
        
        int endIndex = m_ItemPosAndSizeList.Count - 1;
        for (int i = 1; i <= endIndex; ++i)
        {
            tmpBottomDis = m_ItemPosAndSizeList[i].bottomDis;
            if (scrollDis <= tmpBottomDis)
                return i;
        }

        return endIndex;
    }

    private float ViewportSize_Vertical
    {
        get
        {
            var viewport = m_ScrollRect.viewport;
            float size = viewport.rect.height;
            return size;
        }
    }

    private float ViewportBottomDis
    {
        get
        {
            float ret = ScrollDistance_Vertical + ViewportSize;
            return ret;
        }
    }
    
    private float ScrollDistance_Vertical
    {
        get
        {
            var content = m_ScrollRect.content;
            float scrollDis = ListViewUtil.GetLeftTopCornerLPos(content).y;
            return scrollDis;
        }
    }
    
    // -------------------- #region 向上滑动

    private void CheckAndRecycleTopItems_Vertical(float scrollDis)
    {
        //顶部的回收: 从前往后检查, 从0条回收到哪条
        Debug.Log($"========== scrollUp: check recycle");
        int endItemNodeIndex = m_ItemNodeList.Count - 1;
        int recycleItemNodeIndex = endItemNodeIndex;
        for (int i = 0; i <= endItemNodeIndex; ++i)
        {
            var itemNode = m_ItemNodeList[i];
            var itemPosAndSize = m_ItemPosAndSizeList[itemNode.dataIndex];
            Debug.Log($"itemNodeIndex:{i} -> {itemNode.rtf.name}, bottomDis({itemPosAndSize.bottomDis}) >= scrollDis({scrollDis}) ?");
            if (itemPosAndSize.bottomDis >= scrollDis)
            {
                recycleItemNodeIndex = i - 2;
                Debug.Log($"recycleItemNodeIndex: {recycleItemNodeIndex}");
                break;
            }
        }
        Debug.Log($"==========");
        
        if (recycleItemNodeIndex >= 0)
        {
            m_LateUpdateFlag = 2;
            m_AutoLayoutFlag = 1;
            m_AdjustBeginStubSizeFlag = true; //顶部有条目回收, 占位节点大小变大
            
            for (int i = recycleItemNodeIndex; i >= 0; --i)
            {
                var tmpItemNode = m_ItemNodeList[i];
                m_ItemNodeList.RemoveAt(i);
                RecycleItem(tmpItemNode.rtf);
            }
        }
    }
    
    private void CheckAndFillBottomItems_Vertical(float viewportBottomDis)
    {
        var content = m_ScrollRect.content;
        
        //底部不够的补充
        var lastItemNode = m_ItemNodeList[m_ItemNodeList.Count - 1];
        var lastItemPosAndSize = m_ItemPosAndSizeList[lastItemNode.dataIndex];
        float tmpTopDis = lastItemPosAndSize.TopDis; //GetItemNodeTopDis(itemNode_2);
        float tmpBottomDis = 0;
        
        float prevItemSize = lastItemPosAndSize.size; // itemNode_2.rect.height;
        int tmpDataIndex = lastItemNode.dataIndex; //GetDataIndexByDis(tmpTopDis);
        bool isFillBottomItems = false;
        while (tmpTopDis <= viewportBottomDis)
        {
            tmpDataIndex++;
            if (tmpDataIndex >= m_DataCount)
                break;
                    
            var newItemRtf = m_OnObtainItem(this, tmpDataIndex);
            if (null == newItemRtf)
                break;
            isFillBottomItems = true;
            newItemRtf.gameObject.SetActive(true);
            newItemRtf.SetParent(content, false);
            newItemRtf.SetAsLastSibling();
            m_ItemNodeList.Add(new ItemNode(newItemRtf, tmpDataIndex));

            tmpTopDis += prevItemSize;
            tmpTopDis += m_ItemSpace;
                    
            prevItemSize = newItemRtf.rect.height;
            tmpBottomDis = tmpTopDis + prevItemSize;
            m_ItemPosAndSizeList[tmpDataIndex] = new ItemPosAndSize(prevItemSize, tmpBottomDis); // tmpBottomDis;
            Debug.Log($"scrollUp: {Time.frameCount}: FillBottom: obtainItem: {tmpDataIndex}, {newItemRtf.name}, topDis:{tmpTopDis}, bottomDis:{tmpBottomDis}, lastItem.topDis:{lastItemNode.dataIndex}_{lastItemPosAndSize.TopDis}");
        }

        if (isFillBottomItems)
        {
            m_LateUpdateFlag = 2;
            m_AutoLayoutFlag = 1;
            m_AdjustEndStubSizeFlag = true; //底部有条目生成, 占位节点大小变小
            m_EndStub.SetAsLastSibling();
        }
    }
    
    // -------------------- #regionend 向上滑动

    // -------------------- #region 向下滑动
    
    private void CheckAndRecycleBottomItems_Vertical(float viewportBottomDis)
    {
        //底部的回收: 从后往前检查, 从哪条开始回收到结束
        Debug.Log($"========== scrollDown: check recycle");
        int endItemNodeIndex = m_ItemNodeList.Count - 1;
        int recycleItemNodeIndex = 0;
        for (int i = endItemNodeIndex; i >= 0; --i)
        {
            var itemNode = m_ItemNodeList[i];
            var itemPosAndSize = m_ItemPosAndSizeList[itemNode.dataIndex];
            float topDis = itemPosAndSize.TopDis;
            Debug.Log($"itemNodeIndex:{i} -> {itemNode.rtf.name}, topDis({topDis}) <= vpBottomDis({viewportBottomDis})?");
            if (topDis <= viewportBottomDis)
            {
                recycleItemNodeIndex = i + 2;
                Debug.Log($"recycleItemNodeIndex: {recycleItemNodeIndex}");
                break;
            }
        }
        Debug.Log($"==========");

        float tmpTopDis = 0;
        if (recycleItemNodeIndex >= 0 && recycleItemNodeIndex <= endItemNodeIndex)
        {
            m_LateUpdateFlag = 2;
            m_AutoLayoutFlag = 1;
            m_AdjustEndStubSizeFlag = true; //底部有条目回收, 占位节点大小变大
            
            var itemNode = m_ItemNodeList[recycleItemNodeIndex];
            Debug.Log($"scrollDown: {Time.frameCount}: recycleBottom: recycleItemNodeIndex:{recycleItemNodeIndex}~{endItemNodeIndex}, {itemNode.rtf.name}, tmpTopDis:{tmpTopDis}");
            Debug.Log($"scrollDown: recycleBottom: recycleDataIndex:{m_ItemNodeList[recycleItemNodeIndex].dataIndex}~{m_ItemNodeList[endItemNodeIndex].dataIndex}");
            
            for (int i = endItemNodeIndex; i >= recycleItemNodeIndex; --i)
            {
                var tmpItemNode = m_ItemNodeList[i];
                m_ItemNodeList.RemoveAt(i);
                RecycleItem(tmpItemNode.rtf);
            }
        }
    }

    private void CheckAndFillTopItems_Vertical(float scrollDis)
    {
        var content = m_ScrollRect.content;
        
        //顶部不够的补充
        var firstItemNode = m_ItemNodeList[0];
        var firstItemPosAndSize = m_ItemPosAndSizeList[firstItemNode.dataIndex];

        float prevItemSize = firstItemPosAndSize.size;
        float tmpTopDis = firstItemPosAndSize.TopDis;// GetItemNodeTopDis(itemNode_2);
        float tmpBottomDis = firstItemPosAndSize.bottomDis; //条目bottom与content top的距离
        int tmpDataIndex = firstItemNode.dataIndex; //GetDataIndexByDis(tmpBottomDis);
        bool isFillTopItems = false;
        while (tmpDataIndex > 0 && tmpBottomDis >= scrollDis) //第1个VisibleItem的top进入视口了
        {
            tmpDataIndex -= 1;
            var newItemRtf = m_OnObtainItem(this, tmpDataIndex);
            if (null == newItemRtf)
                break;
            isFillTopItems = true;
            newItemRtf.gameObject.SetActive(true);
            newItemRtf.SetParent(content, false);
            newItemRtf.SetAsFirstSibling();
            m_ItemNodeList.Insert(0, new ItemNode(newItemRtf, tmpDataIndex));

            tmpBottomDis -= prevItemSize;
            tmpBottomDis -= m_ItemSpace;
            
            prevItemSize = newItemRtf.rect.height;
            m_ItemPosAndSizeList[tmpDataIndex] = new ItemPosAndSize(prevItemSize, tmpBottomDis);
            Debug.Log($"scrollDown: {Time.frameCount}:  obtainItem: {tmpDataIndex}, bottomDis:{tmpBottomDis}");
            tmpTopDis = tmpBottomDis - prevItemSize;
        }

        if (isFillTopItems)
        {
            m_LateUpdateFlag = 2;
            m_AutoLayoutFlag = 1;
            m_AdjustBeginStubSizeFlag = true; //顶部有条目生成, 占位节点大小变下
            
            if (tmpDataIndex >0)
            {
                m_BeginStub.SetAsFirstSibling();
            }
        }
    }
    
    // -------------------- #regionend 向下滑动

    private void OnValueChanged_ScrollRect_Vertical(Vector2 vec2)
    {
        //左, 下是(0, 0)
        int v = Mathf.RoundToInt(vec2.y * 100);
        int scrollPercent = 100 - v;
        CheckScrollPercent_Vertical(scrollPercent);
    }
    
    private void CheckScrollPercent_Vertical(int scrollPercent)
    {
        int oldScrollPercent = m_ScrollPercent;
        
        float scrollDis = ScrollDistance_Vertical;
        int deltaScrollPercent = scrollPercent - oldScrollPercent;
        //Debug.Log($"OnValueChanged_ScrollRect_Vertical: scrollDis:{scrollDis}, scrollPer:{scrollPercent}, oldScrollPer:{oldScrollPercent}, dtScrollPer:{deltaScrollPercent}");
        if (deltaScrollPercent > 0) //往上滑动
        {
            m_ScrollPercent = scrollPercent;
            float viewportBottomDis = scrollDis + ViewportSize; //viewport bottom与content top的距离
            
            if (oldScrollPercent >= 0 && oldScrollPercent <= 100) //滑动前, 没有overScroll
            {
                CheckAndRecycleTopItems_Vertical(scrollDis);
            }
            if (m_ItemNodeList.Count > 0)
            {
                CheckAndFillBottomItems_Vertical(viewportBottomDis);
                if (m_AdjustBeginStubSizeFlag) //顶部有回收
                {
                    m_AdjustBeginStubSizeFlag = false;
                    AdjustBeginStubSize();
                }
                if (m_AdjustEndStubSizeFlag) //底部有生成
                {
                    m_AdjustEndStubSizeFlag = false;
                    AdjustItemsPosAfter_Vertical(m_ItemNodeList.Count - 1);
                    AdjustEndStubSize();
                }
            }
            else
            {
                //全部被回收了, 确定scrollDis处对应的条目, 并开始填充
                int dataIndex = GetDataIndexByScrollDis_Vertical(scrollDis);
                InitItems_Vertical(dataIndex);
                if (m_ItemNodeList.Count > 0)
                {
                    AdjustBeginStubSize();
                    AdjustItemsPosAfter_Vertical(m_ItemNodeList.Count - 1);
                    AdjustEndStubSize();
                }
            }
        }
        else if (deltaScrollPercent < 0) //往下滑动
        {
            m_ScrollPercent = scrollPercent;
            float viewportBottomDis = scrollDis + ViewportSize; //viewport bottom与content top的距离
            
            if (oldScrollPercent >= 0 && oldScrollPercent <= 100) //滑动前, 没有overScroll
            {
                CheckAndRecycleBottomItems_Vertical(viewportBottomDis);
            }
            if (m_ItemNodeList.Count > 0)
            {
                CheckAndFillTopItems_Vertical(scrollDis);
                if (m_AdjustBeginStubSizeFlag) //顶部有生成
                {
                    m_AdjustBeginStubSizeFlag = false;
                    AdjustBeginStubSize();
                    AdjustItemsPosFromFirstItemNode_Vertical();
                }
                if (m_AdjustEndStubSizeFlag) //底部有回收
                {
                    m_AdjustEndStubSizeFlag = false;
                    AdjustEndStubSize();
                }
            }
            else
            {
                //全部被回收了, 确定scrollDis处对应的条目, 并开始填充
                int dataIndex = GetDataIndexByScrollDis_Vertical(scrollDis);
                InitItems_Vertical(dataIndex);
                if (m_ItemNodeList.Count > 0)
                {
                    AdjustBeginStubSize();
                    AdjustItemsPosAfter_Vertical(m_ItemNodeList.Count - 1);
                    AdjustEndStubSize();
                }
            }
        }
    }

    private bool SyncItemNodeSizeForAutoLayout_Vertical()
    {
        bool isAnyItemSizeChange = false;
        foreach (var itemNode in m_ItemNodeList)
        {
            float itemSize = itemNode.rtf.rect.height;
            //float itemBottomDis = ListViewUtil.GetItemNodeTopDis(itemNode.rtf) + itemSize;

            var itemPosAndSize = m_ItemPosAndSizeList[itemNode.dataIndex];
            if (!isAnyItemSizeChange)
            {
                float delta = itemSize - itemPosAndSize.size;
                if (Mathf.Abs(delta) >= 0.001f)
                {
                    isAnyItemSizeChange = true;
                }
            }
            itemPosAndSize.size = itemSize;
            m_ItemPosAndSizeList[itemNode.dataIndex] = itemPosAndSize;
        }

        return isAnyItemSizeChange;
    }

    private bool CheckIsAnyItemNodePosAndSizeNotSync_Vertical()
    {
        foreach (var itemNode in m_ItemNodeList)
        {
            float itemSize = itemNode.rtf.rect.height;
            float itemBottomDis = ListViewUtil.GetItemNodeTopDis(itemNode.rtf) + itemSize;

            var itemPosAndSize = m_ItemPosAndSizeList[itemNode.dataIndex];
            float delta = itemSize - itemPosAndSize.size;
            if (Mathf.Abs(delta) >= 0.001f)
            {
                return true;
            }

            delta = itemBottomDis - itemPosAndSize.bottomDis;
            if (Mathf.Abs(delta) >= 0.001f)
            {
                return true;
            }
        }
        return false;
    }

    private void UpdateItemsPosAndSize_Vertical()
    {
        Debug.Log($"UpdateItemsPosAndSize_Vertical: contentSize:{m_ScrollRect.content.rect.height}");
        int endItemNodeIndex = m_ItemNodeList.Count - 1;
        if (endItemNodeIndex < 0)
            return;

        switch (m_AutoLayoutFlag)
        {
            case 1: //校验测算的大小和AutoLayout后是否一致, 不一致同步一下并重新测算
            {
                if (CheckIsAnyItemNodePosAndSizeNotSync_Vertical())
                {
                    m_LateUpdateFlag = 2;
                    m_AutoLayoutFlag = 2;
                    Debug.Log($"UpdateItemsPosAndSize_Vertical: verify fail, adjust again");
                }
                else
                {
                    PrintAllItemPosAndSize("UpdateItemsPosAndSize");
                    m_AutoLayoutFlag = 0;
                }
                break;
            }
            
            case 2: //校验失败时, 同步大小并重新测算Pos
            {
                Debug.Log($"UpdateItemsPosAndSize_Vertical: adjust");
                bool isAnyItemSizeChange = SyncItemNodeSizeForAutoLayout_Vertical();
                AdjustAllItemPos_Vertical();
                AdjustBeginStubSize();
                AdjustEndStubSize();
                m_AutoLayoutFlag = 1; //再验证一次
                m_LateUpdateFlag = 2;
                break;
            }
        }
    }
    
    private void AdjustAllItemPos_Vertical()
    {
        float tmpTopDis = m_ItemSpace;
        for (int i = 0; i < m_DataCount; ++i)
        {
            var itemPosAndSize = m_ItemPosAndSizeList[i];
            float bottomDis = tmpTopDis + itemPosAndSize.size;
            itemPosAndSize.bottomDis = bottomDis;
            m_ItemPosAndSizeList[i] = itemPosAndSize;

            tmpTopDis = bottomDis + m_ItemSpace;
        }
    }
    
    private void AdjustItemsPosFromFirstItemNode_Vertical()
    {
        var firstItemNode = m_ItemNodeList[0];
        float tmpTopDis = 0;
        if (0 == firstItemNode.dataIndex)
        {
            tmpTopDis = m_LeadPadding;
        }
        else
        {
            tmpTopDis = m_ItemPosAndSizeList[firstItemNode.dataIndex - 1].bottomDis;
            tmpTopDis += m_ItemSpace;
        }
        for (int i = firstItemNode.dataIndex; i < m_DataCount; ++i)
        {
            var itemPosAndSize = m_ItemPosAndSizeList[i];
            float bottomDis = tmpTopDis + itemPosAndSize.size;
            itemPosAndSize.bottomDis = bottomDis;
            m_ItemPosAndSizeList[i] = itemPosAndSize;
            
            tmpTopDis = bottomDis + m_ItemSpace;
        }
    }
    
    private void AdjustItemsPosAfter_Vertical(int endItemNodeIndex)
    {
        var endItemNode = m_ItemNodeList[endItemNodeIndex];
        float tmpBottomDis = m_ItemPosAndSizeList[endItemNode.dataIndex].bottomDis;
        for (int i = endItemNode.dataIndex + 1; i < m_DataCount; ++i)
        {
            float topDis = tmpBottomDis + m_ItemSpace;
            
            var itemPosAndSize = m_ItemPosAndSizeList[i];
            tmpBottomDis = topDis + itemPosAndSize.size;
            itemPosAndSize.bottomDis = tmpBottomDis;
            m_ItemPosAndSizeList[i] = itemPosAndSize;
        }
    }
    
}
